package com.whoimi.admin.service.impl;

import com.whoimi.admin.entity.Notice;
import com.whoimi.admin.entity.UserNotice;
import com.whoimi.admin.entity.vo.UserNoticeVO;
import com.whoimi.admin.repository.NoticeRepository;
import com.whoimi.admin.repository.UserNoticeRepository;
import com.whoimi.admin.service.UserNoticeService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * @author whoimi
 * @since 2025-07-29
 **/
@Service
public class UserNoticeServiceImpl implements UserNoticeService {
    @Resource
    private UserNoticeRepository userNoticeRepository;
    @Resource
    private NoticeRepository noticeRepository;

    @Override
    public Page<UserNotice> page(Pageable pageable) {
//        Page<UserNotice> all = userNoticeRepository.findUserNoticesWithDetails(2L, pageable);
        Page<UserNotice> all = userNoticeRepository.findByUsername("admin", pageable);
        return all;
    }

    @Override
    public Page<UserNoticeVO> transPage(Pageable pageable) {
        Page<UserNotice> userNoticePage = userNoticeRepository.findByUsername("admin", pageable);
        List<Long> noticeIds = userNoticePage.getContent().stream()
                .map(UserNotice::getNoticeId)
                .filter(Objects::nonNull)
                .toList();
        Iterable<Notice> noticeIterable = noticeRepository.findAllById(noticeIds);
        // 转换为Map，处理可能的重复key
        Map<Long, Notice> noticeMap = StreamSupport.stream(noticeIterable.spliterator(), false)
                .collect(Collectors.toMap(
                        Notice::getId,
                        notice -> notice,
                        (existing, replacement) -> existing
                ));
        // 组合UserNotice和Notice信息到UserNoticeVO
        List<UserNoticeVO> userNoticeVOList = userNoticePage.getContent().stream()
                .map(userNotice -> {
                    UserNoticeVO vo = new UserNoticeVO();

                    // 设置UserNotice相关信息
                    vo.setId(userNotice.getId());
                    vo.setIsRead(userNotice.getIsRead());
//                    vo.setReadTime(userNotice.getReadTime());
//                    vo.setCreateTime(userNotice.getCreateTime());
//                    vo.setUpdateTime(userNotice.getUpdateTime());
//                    vo.setDeleted(userNotice.getIsDeleted());

                    // 获取并设置对应的Notice信息
                    Long noticeId = userNotice.getNoticeId();
                    Notice notice = noticeMap.get(noticeId);
                    if (notice != null) {
                        vo.setTitle(notice.getTitle());
                        vo.setType(notice.getType());
                        vo.setLevel(notice.getLevel());
                        vo.setPublisher(notice.getPublisher() != null ?
                                notice.getPublisher().toString() : null);
                        vo.setPublishTime(notice.getPublishTime());
                    }

                    return vo;
                })
                .toList();

        return new PageImpl<>(userNoticeVOList, pageable, userNoticePage.getTotalElements());
    }

    @Override
    public Long markAsRead(String username) {
        return userNoticeRepository.markAsRead(username);
    }
}
